/*0GI1xcoeligdpMeXoHBphqKrts2icNrgDxjZYHOrklgKSMhsahsjLUN5r7svr5FP*/
import {ajax} from 'nc-lightapp-front';
import constants from "../../constants";
import {linkToCard, deleteLine} from "./buttonClick";
import ampub from 'ampub';
import fa from 'fa';
const {utils} = ampub;
const {getMultiLangByID} = utils.multiLangUtils;
const {showMessage} = utils.msgUtils;

const {fa_components} = fa;
const {CardLinkToBill} = fa_components.CardLinkToBillUtil;

const headPk = constants.FIELDS.HEAD_PK; //主实体 pk 字段
const bodyPk = constants.FIELDS.BODY_PK; //子实体 pk 字段

export default function (props, key, text, record, index) {
    switch (key) {
        case constants.LIST_BTNS.TABLE_BODY.EDIT:
            edit(props, record);
            break;

        case  constants.LIST_BTNS.TABLE_BODY.COPY:
            doCopyOnTable(this, record);
            break;

        case  constants.LIST_BTNS.TABLE_BODY.DELETE:
            deleteLine(props, {values: record});
            break;

        case  constants.LIST_BTNS.TABLE_BODY.QueryAboutBusiness:
            billreview(props, record);
            break;

        default:
            break;
    }
}

function edit(props, record) {
    const pk_card = record[headPk].value;
    const promise = new Promise(function (resolve, reject) {
        if (pk_card) {
            ajax({
                url: constants.URL.EDIT,
                data: {
                    pk: pk_card,
                    resourceCode: '2012016030', //固定资产卡片维护和原始卡片走同一个资源号
                },
                success: (res) => {
                    let {success, data} = res;
                    if (success) {
                        let isValidate = data['validate'];
                        let failure = data['failure'];

                        if (failure) {
                            reject(failure);
                            return;
                        }
                        if (!isValidate) {
                            reject(isValidate);
                            return;
                        }
                        resolve(data);
                    }
                },
                error: (err) => {
                    reject(err);
                }
            });
        }
    }).then((resData) => {
            //卡片校验通过，可以编辑
            linkToCardOnRow(props, record, constants.UISTATE.EDIT);
        },
        (err) => {
            showMessage(props, {content: err.message || err || getMultiLangByID('201201504A-000013'), color: 'danger'});
        });
    
}

//table 行点击的跳转到卡片态
function linkToCardOnRow(props, record, status) {
    const id = record[bodyPk].value;
    const param = {
        status,
        id,
        transiType: record.transi_type.value,
        pagecode: record.transi_type.value,
    };
    linkToCard(props, param);
}

//表格操作列 copy
function doCopyOnTable(that, record) {
    const pk_card = record.pk_card.value;
    that.facardCopy.showCopyCardDialog(that, pk_card);
}

//单据追溯
function billreview(props, record) {
    //获取卡片来源
    let bill_source = record.bill_source && record.bill_source.value;
    //获取来源主键
    let pk_bill_src = record.pk_bill_src && record.pk_bill_src.value;
    if (pk_bill_src) {
        if (!(bill_source === 'handin')) {
            CardLinkToBill(props, pk_bill_src, bill_source);
        }
    }
}

export {
    linkToCardOnRow
}




/*0GI1xcoeligdpMeXoHBphqKrts2icNrgDxjZYHOrklgKSMhsahsjLUN5r7svr5FP*/